<template>
	<view class="detail-container">
		<u-tabs :list="tabList" @click="click" :scrollable="false" :current="current"></u-tabs>
		<scroll-view scroll-y class="detail-content">
			<basic-info
				v-show="current == 0"
				ref="detailTab"
				class="detail-tab"
				@closeellipsistip="close"
				@opentips="openTips"
				@close="close"
				:detailInfo="detailInfo"
				@refreshlist="getDetail"
			></basic-info>
			<career class="detail-tab" v-show="current == 1" :careerInfo="careerInfo" @refreshlist="refreshCareer"></career>
			<salary-flow class="detail-tab" v-show="current == 2" :salaryFlowInfo="salaryFlowInfo" @getlist="getSalaryDetailList" @refreshlist="refreshSalaryDetail"></salary-flow>
			<!-- <up-tabs
				class="custom-class"
				:active="active"
				animated
				color="#3F64FA"
				title-active-color="#3F64FA"
				title-inactive-color="#4B5680"
				@change="onChange"
				swipeable
				border
				:lazy-render="false"
			>
				<up-tab title="基本信息" class="tab" name="baseInfo">
					<basic-info class="detail-tab" @closeellipsistip="close" @opentips="openTips" @close="close" :detailInfo="detailInfo" @refreshlist="getDetail"></basic-info>
				</up-tab>
				<up-tab title="教育培训" class="tab" name="safetyEducation">
					<safety-education
						class="detail-tab"
						safetyEducationInfo="{{safetyEducationInfo}}"
						bindrefreshlist="refreshTrainRecord"
						bindgetlist="getTrainRecord"
					></safety-education>
				</up-tab>
				<up-tab title="职业生涯" class="tab" name="career">
					<career class="detail-tab" :careerInfo="careerInfo" @refreshlist="refreshCareer"></career>
				</up-tab>
				<up-tab title="工资流水" class="tab" name="salaryFlow">
					<salary-flow class="detail-tab" :salaryFlowInfo="salaryFlowInfo" @getlist="getSalaryDetailList" @refreshlist="refreshSalaryDetail"></salary-flow>
				</up-tab>
			</up-tabs> -->
		</scroll-view>
		<ellipsis-tip ref="ellipsisTip" :content="tipContent"></ellipsis-tip>
	</view>
</template>

<script>
import basicInfo from '@/pagesA/components/roster/basic-info/basic-info';
import career from '@/pagesA/components/roster/career/career';
import ellipsisTip from '@/pagesA/components/common/ellipsis-tip/ellipsis-tip';
import safetyEducation from '@/pagesA/components/roster/safety-education/safety-education';
import salaryFlow from '@/pagesA/components/roster/salary-flow/salary-flow';
import { common } from '@/pagesA/utils/common.js';
import moment from 'moment';
import { workingStatusList } from '@/pagesA/config/enum';
const app = getApp();
export default {
	components: {
		basicInfo,
		career,
		ellipsisTip,
		safetyEducation,
		salaryFlow
	},
	data() {
		return {
			active: 'baseInfo',
			uuid: '',
			projectUuid: '',
			detailInfo: {},
			careerInfo: {},
			safetyEducationInfo: {
				avatar: '',
				recordList: []
			},
			salaryFlowInfo: {
				avatar: '',
				salaryDetailList: []
			},
			workingStatusList,
			tipContent: '',
			isScanCode: false,
			joinProjectUuid: '',
			//扫工友码，非本项目管理人员邀请加入其他项目的项目ID
			isCurProject: true,
			isManager: false,
			trainRecordPageNum: 1,
			trainRecordPageSize: 20,
			loading: false,
			current: 0,
			tabList: [
				{
					name: '基本信息'
				},
				{
					name: '职业生涯'
				},
				{
					name: '工资流水'
				}
			]
		};
	},
	onLoad(options) {
		// let active = options.active || 'baseInfo';
		// this.setData({
		// 	active
		// });
		this.current = options.current || 0;
		if (options.scene) {
			//扫码进入
			this.getWorkbench(() => {
				this.resolveScene(options.scene);
			});
		} else {
			this.uuid = options.uuid;
			this.projectUuid = options.projectUuid;
			this.getDetail();
		}
	},
	onShow() {
		if (app.globalData.isModify) {
			//修改花名册
			this.getDetail();
			delete app.globalData.isModify;
		}
	},
	methods: {
		click(item) {
			this.current = item.index;
		},
		toErrTip(err) {
			app.globalData.errorTip = err;
			app.globalData.pageTitle = '工友码';
			uni.redirectTo({
				url: '/pagesA/pages/errorTip/errorTip'
			});
		},
		resolveScene(scene) {
			common.apis.roster
				.resolveScene({
					scene
				})
				.then(
					(res) => {
						if (!res.data) {
							return;
						}
						this.uuid = res.data.rosterUuid;
						this.projectUuid = res.data.projectUuid;
						this.isScanCode = true;
						this.checkManager();
					},
					(err) => {
						uni.hideLoading();
						this.toErrTip(err);
					}
				);
		},
		openTips(res) {
			// this.setData({
			// 	tipContent: res.detail.content
			// });
			this.tipContent = res.detail.content;
			// this.zpSelectComponent('#ellipsis-tip').openTips(res.detail.top);
			this.$refs.ellipsisTip.openTips(res.detail.top);
		},
		close() {
			// this.zpSelectComponent('#ellipsis-tip').closeTip();
			this.$refs.ellipsisTip.closeTip();
		},
		handlSex(value) {
			if (value == 0) {
				return '男';
			} else if (value == 1) {
				return '女';
			}
			return;
		},
		handlTime(value, format) {
			return value ? moment(value).format(format) : '';
		},
		handleNumber(val, num) {
			let word = '';
			if (val) {
				word = val.substr(0, num);
				for (let i = num; i < val.length; i++) {
					word += '*';
				}
			}
			return word;
		},
		handleWorkingStatus(workingStatus) {
			for (let item of this.workingStatusList) {
				if (workingStatus == item.code) {
					return item.value;
				}
			}
			return;
		},
		handleFileUrl(url) {
			let word = '';
			if (!url) {
				return word;
			}
			let keyLen = url.length;
			let leftNum = 22;
			let rightNum = 3;
			if (keyLen > leftNum + rightNum) {
				word += url.substring(0, leftNum);
				word += '...';
				word += url.substring(keyLen - rightNum, keyLen + 1);
			}
			return word;
		},
		handleCertificateList(certificateList) {
			for (let i = 0; i < certificateList.length; i++) {
				certificateList[i].indexData = '证书' + common.commonMethods.toChinesNum(i + 1);
				certificateList[i].issuedAtData = this.handlTime(certificateList[i].issuedAt, 'YYYY/MM/DD');
				certificateList[i].expiredAtData = this.handlTime(certificateList[i].expiredAt, 'YYYY/MM/DD');
			}
			return certificateList;
		},
		getWorkbench(fn) {
			common.apis.home.getWorkbench().then((res) => {
				let appList = res.data?.apps ?? [];
				let cisdiOpearList = [];
				for (let newApp of appList) {
					cisdiOpearList.push(...newApp.opts);
				}
				uni.setStorageSync('cisdiOpearList', cisdiOpearList);
				if (fn) {
					fn();
				}
			});
		},
		handleAuth(code) {
			let cisdiOpearList = uni.getStorageSync('cisdiOpearList');
			let hasAuth = false;
			if (cisdiOpearList) {
				for (let item of cisdiOpearList) {
					if (item == code) {
						hasAuth = true;
						break;
					}
				}
			}
			return hasAuth;
		},
		checkManager() {
			common.apis.roster
				.checkManager({
					projectUuid: this.projectUuid
				})
				.then(
					(res) => {
						if (!res.data) {
							return;
						}
						// this.setData({
						// 	isCurProject: res.data.isCurProject,
						// 	isManager: res.data.isManager,
						// 	joinProjectUuid: res.data.curProjectUuid
						// });
						this.isCurProject = res.data.isCurProject;
						this.isManager = res.data.isManager;
						this.joinProjectUuid = res.data.curProjectUuid;
						this.getDetail();
					},
					(err) => {
						uni.hideLoading();
						this.toErrTip(err);
					}
				);
		},
		getDetail() {
			common.apis.roster
				.getDetail({
					uuid: this.uuid,
					projectUuid: this.projectUuid
				})
				.then(
					(res) => {
						if (!res.data) {
							return;
						}
						let detailInfo = res.data;
						detailInfo.identityFrontPhotoData = common.commonMethods.imgShow(detailInfo.identityFrontPhoto);
						detailInfo.identityBackPhotoData = common.commonMethods.imgShow(detailInfo.identityBackPhoto);
						detailInfo.avatarData = common.commonMethods.imgShow(detailInfo.avatar);
						detailInfo.lastestAttendanceRecordData = this.handlTime(detailInfo.attendanceMap.lastestAttendanceRecord, 'YYYY/MM/DD HH:mm:ss');
						detailInfo.sexData = this.handlSex(detailInfo.sex);
						detailInfo.birthdayData = this.handlTime(detailInfo.birthday, 'YYYY/MM/DD');
						detailInfo.issuedAtData = this.handlTime(detailInfo.issuedAt, 'YYYY.MM.DD');
						detailInfo.expiredAtData = this.handlTime(detailInfo.expiredAt, 'YYYY.MM.DD');
						detailInfo.phoneData = this.handleNumber(detailInfo.phone, 3);
						detailInfo.workingStatusData = this.handleWorkingStatus(detailInfo.workingStatus);
						detailInfo.startDateData = this.handlTime(detailInfo.startDate, 'YYYY/MM/DD');
						detailInfo.leaveDateData = this.handlTime(detailInfo.leaveDate, 'YYYY/MM/DD');
						detailInfo.entryAgreementData = this.handleFileUrl(detailInfo.entryAgreement);
						detailInfo.departureAgreementData = this.handleFileUrl(detailInfo.departureAgreement);
						detailInfo.contractAttachmentData = this.handleFileUrl(detailInfo.contractAttachment);
						detailInfo.certificateList = this.handleCertificateList(detailInfo.certificateList);
						detailInfo.loading = true;
						detailInfo.joinProjectUuid = this.joinProjectUuid;
						detailInfo.editBtnAuth = this.handleAuth('20131063'); //判断是否有编辑操作
						detailInfo.isManager = this.isManager;
						detailInfo.isCurProject = this.isCurProject;
						detailInfo.isScanCode = this.isScanCode;
						detailInfo.identityNumberData = this.handleNumber(detailInfo.identityNumber, 6);
						detailInfo.isPullRefresh = false;
						if (this.isScanCode && detailInfo.isBlack) {
							// this.zpSelectComponent('.detail-tab').openDialog(detailInfo);
							this.$refs.detailTab.openDialog(detailInfo);
						}
						// this.setData({
						// 	detailInfo
						// });
						this.detailInfo = detailInfo;
						this.getCareer(detailInfo.personType.code);
						// this.refreshTrainRecord();
						this.refreshSalaryDetail();
					},
					() => {
						let detailInfo = {};
						detailInfo.isPullRefresh = false;
						// this.setData({
						// 	detailInfo
						// });
						this.detailInfo = detailInfo;
					}
				);
		},
		refreshCareer(e) {
			let personType = e.detail.personType;
			this.getCareer(personType);
		},
		getCareer(personType) {
			common.apis.roster
				.getCareer({
					uuid: this.uuid,
					projectUuid: this.projectUuid
				})
				.then(
					(res) => {
						if (!res.data) {
							return;
						}
						let careerInfo = res.data;
						careerInfo.avatarData = common.commonMethods.imgShow(careerInfo.avatar);
						careerInfo.personType = personType;
						careerInfo.finish = true;
						careerInfo.isPullRefresh = false;
						// this.setData({
						// 	careerInfo
						// });
						this.careerInfo = careerInfo;
					},
					() => {
						let careerInfo = {};
						careerInfo.isPullRefresh = false;
						// this.setData({
						// 	careerInfo
						// });
						this.careerInfo = careerInfo;
					}
				);
		},
		handleWorkType(personType) {
			if (personType == '10') {
				return '管理人员';
			} else if (personType == '30') {
				return '班组长';
			} else {
				return;
			}
		},
		handleSafetyEducationInfo(safetyEducationInfo, recordList) {},
		refreshTrainRecord() {
			// this.setData({
			// 	trainRecordPageNum: 1,
			// 	trainRecordPageSize: 20
			// });
			this.trainRecordPageNum = 1;
			this.trainRecordPageSize = 20;
			this.safetyEducationInfo = {
				avatar: '',
				recordList: []
			};
			this.getTrainRecord();
		},
		getTrainRecord() {
			common.apis.roster
				.getTrainRecord({
					uuid: this.uuid,
					projectUuid: this.projectUuid,
					pageNum: this.trainRecordPageNum,
					pageSize: this.trainRecordPageSize
				})
				.then(
					(res) => {
						if (!res.data) {
							return;
						}
						let safetyEducationInfo = res.data;
						safetyEducationInfo.workTypeName = this.handleWorkType(res.data.personType) || safetyEducationInfo.worktypeOrStationName;
						let recordList = [];
						for (let item of safetyEducationInfo.recordList) {
							item.editTime = this.handlTime(item.time, 'YYYY/MM/DD');
							recordList.push(item);
						}
						delete safetyEducationInfo.recordList;
						this.safetyEducationInfo.recordList = this.safetyEducationInfo.recordList.concat(recordList);
						if (
							this.safetyEducationInfo.avatar &&
							safetyEducationInfo.avatar &&
							common.commonMethods.isSameAvatar(this.safetyEducationInfo.avatar, safetyEducationInfo.avatar)
						) {
							safetyEducationInfo.avatar = this.safetyEducationInfo.avatar;
						}
						Object.assign(this.safetyEducationInfo, safetyEducationInfo);
						// this.setData({
						// 	safetyEducationInfo: {
						// 		...this.safetyEducationInfo,
						// 		noMore: recordList.length < this.trainRecordPageSize,
						// 		isPullRefresh: false,
						// 		loading: true
						// 	},
						// 	trainRecordPageNum: (this.trainRecordPageNum += 1)
						// });
						this.safetyEducationInfo = {
							...this.safetyEducationInfo,
							noMore: recordList.length < this.trainRecordPageSize,
							isPullRefresh: false,
							loading: true
						};
						this.trainRecordPageNum = this.trainRecordPageNum += 1;
					},
					() => {
						// this.setData({
						// 	safetyEducationInfo: {
						// 		...this.safetyEducationInfo,
						// 		noMore: false,
						// 		isPullRefresh: false,
						// 		loading: true
						// 	}
						// });
						this.safetyEducationInfo = {
							safetyEducationInfo: {
								...this.safetyEducationInfo,
								noMore: false,
								isPullRefresh: false,
								loading: true
							}
						};
					}
				);
		},
		refreshSalaryDetail(e) {
			this.salaryFlowInfo = {
				avatar: ''
			};
			this.getSalaryDetailList(e);
		},
		getSalaryDetailList(e) {
			// this.setData({
			// 	['salaryFlowInfo.salaryDetailList']: []
			// });
			this.salaryFlowInfo.salaryDetailList = [];
			let year = '';
			if (!e) {
				year = moment().year() + '';
			} else {
				year = e.detail.year;
			}
			common.apis.roster
				.getSalaryDetailList({
					uuid: this.uuid,
					projectUuid: this.projectUuid,
					year
				})
				.then(
					(res) => {
						let salaryFlowInfo = res.data;
						salaryFlowInfo.workTypeName = this.handleWorkType(res.data.personType) || salaryFlowInfo.worktypeOrStationName;
						if (this.salaryFlowInfo.avatar && salaryFlowInfo.avatar && common.commonMethods.isSameAvatar(this.salaryFlowInfo.avatar, salaryFlowInfo.avatar)) {
							salaryFlowInfo.avatar = this.salaryFlowInfo.avatar;
						}
						salaryFlowInfo.avatarData = common.commonMethods.imgShow(salaryFlowInfo.avatar);
						// this.setData({
						// 	salaryFlowInfo: {
						// 		year,
						// 		isPullRefresh: false,
						// 		...salaryFlowInfo
						// 	},
						// 	loading: true
						// });
						this.salaryFlowInfo = {
							year,
							isPullRefresh: false,
							...salaryFlowInfo
						};
						this.loading = true;
					},
					() => {
						// this.setData({
						// 	salaryFlowInfo: {
						// 		year,
						// 		isPullRefresh: false,
						// 		...salaryFlowInfo
						// 	},
						// 	loading: true
						// });
						this.salaryFlowInfo = {
							year,
							isPullRefresh: false,
							...salaryFlowInfo
						};
						this.loading = true;
					}
				);
		}
	}
};
</script>
<style>
.detail-container {
	display: flex;
	flex-direction: column;
	height: 100vh;
}

.detail-content {
	flex: 1;
	height: 0;
	background-color: #f6f7fa;
}
</style>
